//
//  StatusCell.swift
//  WeoboWithSteveLi
//
//  Created by SteveLi on 2017/10/6.
//  Copyright © 2017年 李军桦. All rights reserved.
//

import UIKit


/// 微博cell中控件的间距数值
let StatusCellMargin: CGFloat = 12
/// 微博头像的宽度
let StatusCellIconWidth: CGFloat = 35

/// 微博cell
class StatusCell: UITableViewCell {
    /// 微博视图模型
    var viewModel: StatusViewModel? {
        didSet {
            //设置工作
            topView.viewModel = viewModel
            contentLabel.text = viewModel?.status.text
            //设置配图视图 - 设置视图模型之后，配图视图有能力计算大小
            pictureView.viewModel = viewModel
            //测试修改配图视图的高度 - 实际开发中需要注意，如果修改动态约束的高度，可能会导致行高计算有误 
            pictureView.snp.updateConstraints { (make) in
                //print("配图视图大小\(pictureView.bounds)")
                make.height.equalTo(pictureView.bounds.height)
                //直接设置宽度数值
                make.width.equalTo(pictureView.bounds.width)
                

            }
        }
    }
    
    //根据指定的视图模型计算行高
    func rowHeight(vm: StatusViewModel) -> CGFloat {
        //1.记录视图模型 -> 会调用上面的didSet 设置内容以及更新‘约束’
        viewModel = vm
        //2.强制更新所有约束
        contentView.layoutIfNeeded()
        //3.返回底部视图的最大高度
        return bottomView.frame.maxY
       
       // return  CGRect.maxY(bottomView.frame)

    }
    
    
    //MARK: - 构造函数
    override init(style:UITableViewCellStyle,reuseIdentifier:String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        selectionStyle = .none
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 懒加载控件
    /// 顶部视图
    lazy var topView: StatusCellTopView = StatusCellTopView()
    /// 微博正文标签
    lazy var contentLabel: UILabel = UILabel(title: "微博正文", fontSize: 15, color: UIColor.darkGray, screenInset: StatusCellMargin)
    /// 配图视图
    lazy var pictureView: StatusPictureView = StatusPictureView()
    /// 底部视图
    lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
}

//MARK: - 设置界面
extension StatusCell {
    func setupUI(){
        //1.添加控件
        //1>顶部视图
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        //2.自动布局
        topView.snp.makeConstraints { (make) in
            make.top.equalTo(contentView.snp.top)
            make.left.equalTo(contentView.snp.left)
            make.right.equalTo(contentView.snp.right)
            make.height.equalTo(2*StatusCellMargin + StatusCellIconWidth)
        }
        //2>内容标签
        contentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(topView.snp.bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp.left).offset(StatusCellMargin)
        }

        
        //4.底部视图
        bottomView.snp.makeConstraints { (make) in
            make.top.equalTo(pictureView.snp.bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp.left)
            make.right.equalTo(contentView.snp.right)
            make.height.equalTo(44)
//            //指定向下的约束
//            make.bottom.equalTo(contentView.snp.bottom)
        }
    }
}




